package com.xhs.hawaii.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2018/1/23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {

    @ApiModelProperty(notes = "当前页数")
    private Integer pageNum;

    @ApiModelProperty(notes = "每页数量")
    private Integer pageSize;

    @ApiModelProperty(notes = "总数量")
    private Integer total;

    @ApiModelProperty(notes = "总页数")
    private Integer totalPage;

    @ApiModelProperty(notes = "分页数据")
    private List<T> list;

    public static PageResult of(PageQuery pageQuery, int count, List data) {
        PageResult pageResult = new PageResult();
        pageResult.setList(data);
        pageResult.setPageNum(pageQuery.getPageNum());
        pageResult.setPageSize(pageQuery.getPageSize());
        pageResult.setTotal(count);
        pageResult.setTotalPage(count % pageQuery.getPageSize() == 0 ? count / pageQuery.getPageSize() : count / pageQuery.getPageSize() + 1);
        return pageResult;
    }
}
